using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class TinhTrang_DonHang_DAL
    {
        private SqlConnection cn = null;
        public  TinhTrang_DonHang_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert(TinhTrang_DonHang_DTO TinhTrang_DonHang)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_DonHang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_DH",TinhTrang_DonHang.MaTinhTrang_DH    );
            cm.Parameters.AddWithValue("@TenTinhTrang",TinhTrang_DonHang .TenTinhTrang );
                     
            cm.ExecuteNonQuery();
        }
       public void Update(TinhTrang_DonHang_DTO TinhTrang_DonHang)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_DonHang_Upsate", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_DH", TinhTrang_DonHang .MaTinhTrang_DH  );
            cm.Parameters.AddWithValue("@TenTinhTrang", TinhTrang_DonHang .TenTinhTrang );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_DonHang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTinhTrang_DH", iD);
            cm.ExecuteNonQuery();
        }
       public TinhTrang_DonHangCollection  Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TinhTrang_DonHang_SET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            TinhTrang_DonHangCollection TinhTrangDHCollection = new TinhTrang_DonHangCollection    ();

            while (dr.Read ())
            {
                TinhTrang_DonHang_DTO tinhtrangdh = new TinhTrang_DonHang_DTO();
                tinhtrangdh .MaTinhTrang_DH  = int.Parse(dr.GetValue(0).ToString());
                tinhtrangdh .TenTinhTrang   = dr.GetValue(1).ToString();

                TinhTrangDHCollection.Add(tinhtrangdh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return TinhTrangDHCollection;
        }
    }
}
